package com.goktech.olala.client.controller.basic;

import com.github.pagehelper.PageInfo;

/**
 * 响应给前端的标准结果集
 */
public class ResultData {
    private String msg;// 响应提示信息
    private int code; //响应状态码
    private Object data;// 响应结果数据


    //前端表格插件Datatable 处理分页时所需字段
    private int iTotalRecords; //每页显示的数据大小
    private int draw; // 绘制表格的次数
    private long iTotalDisplayRecords;// 查询的总记录数
    private int sEcho; //每页显示的数据大小


    public ResultData() {
    }

    /**
     * @param code
     * @param msg
     * @param data
     */
    public ResultData(int code, String msg, Object data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    /**
     * @param msg
     * @param code
     * @param data
     * @param iTotalRecords
     * @param draw
     * @param iTotalDisplayRecords
     * @param sEcho
     */
    public ResultData(String msg, int code, Object data, int iTotalRecords, int draw, long iTotalDisplayRecords, int sEcho) {
        this.msg = msg;
        this.code = code;
        this.data = data;
        this.iTotalRecords = iTotalRecords;
        this.draw = draw;
        this.iTotalDisplayRecords = iTotalDisplayRecords;
        this.sEcho = sEcho;
    }

    //    public int getDraw() {
//        return draw;
//    }
//
//    public void setDraw(int draw) {
//        this.draw = draw;
//    }

    public int getsEcho() {
        return 1;
    }

    public void setsEcho(int sEcho) {
        this.sEcho = sEcho;
    }

    public int getiTotalRecords() {
        return iTotalRecords;
    }

    public void setiTotalRecords(int iTotalRecords) {
        this.iTotalRecords = iTotalRecords;
    }

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public long getiTotalDisplayRecords() {
        return iTotalDisplayRecords;
    }

    public void setiTotalDisplayRecords(long iTotalDisplayRecords) {
        this.iTotalDisplayRecords = iTotalDisplayRecords;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 通用响应结果集，操作响应成功时，但是无返回数据时调用
     *
     * @return
     */
    public static ResultData ok() {
        return new ResultData(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMsg(), null);
    }

    public static ResultData ok(Object result) {
        return new ResultData(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMsg(), result);
    }

    /**
     * 通用响应结果集，当成功响应并且有数据时，调用此方法
     *
     * @param data
     * @return
     */
    public static ResultData success(Object data) {
        return new ResultData(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMsg(), data);
    }

    /**
     * 通用响应结果集， 当用户操作失时，调用此方法
     *
     * @return
     */
    public static ResultData error() {
        return new ResultData(ResultStatus.OPERATION_FAIL.getCode(), ResultStatus.OPERATION_FAIL.getMsg(), null);
    }

    /**
     * 通用响应结果集,当用户查询结果为空时，调用此方法
     *
     * @return
     */
    public static ResultData queryFail() {
        return new ResultData(ResultStatus.QUERY_NULL.getCode(), ResultStatus.QUERY_NULL.getMsg(), null);
    }

    /**
     * 自定义响应结果内容
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static ResultData resultData(int code, String msg, Object data) {
        return new ResultData(code, msg, data);
    }

    /**
     * 当给Datatable 插件响应成功数据时，使用此方法
     *
     * @param data
     * @param draw
     * @param recordsTotal
     * @param recordsFiltered
     * @return
     */
    public static ResultData success(Object data, int draw, long recordsTotal, int recordsFiltered) {
        return new ResultData(ResultStatus.SUCCESS.getMsg(), ResultStatus.SUCCESS.getCode(), data, recordsFiltered, draw, recordsTotal, 1);
    }

    //String msg, int code, Object data, int iTotalRecords, int draw, long iTotalDisplayRecords, int sEcho

    public static ResultData success(PageInfo pageInfo, Integer sEcho) {
        Integer pageIndex = pageInfo.getStartRow() / pageInfo.getPageSize();
        return new ResultData(ResultStatus.SUCCESS.getMsg(), ResultStatus.SUCCESS.getCode(), pageInfo.getList(), pageInfo.getPageSize(), pageIndex, pageInfo.getTotal(), sEcho);
    }

    /**
     * int code, String msg, Object data, int draw, long recordsTotal, int recordsFiltered
     */

}
